package com.stox.client.ui.javafx.chartingy.model.node;

import javafx.scene.layout.Region;
import javafx.scene.paint.Color;

import com.stox.client.ui.javafx.chartingy.model.Rectangle;
import com.stox.client.ui.javafx.chartingy.model.plot.Data;
import com.stox.client.ui.javafx.chartingy.model.plot.PricePlot;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.shared.core.model.Bar;
import com.stox.shared.core.model.Scrip;

public class PriceBox extends AbstractPriceNode {
	
	private Region box = new Region();

	public PriceBox(Data<Scrip, Bar> data) {
		super(data);
		UI.style(this, CSS.barNode, CSS.candle);
		getChildren().add(box);
	}
	
	@Override
	public void updateColor(){
		PricePlot plot = (PricePlot)getData().getPlot();
		Color color = null == up ? plot.getColor() : up ? plot.getUpBarColor() : plot.getDownBarColor();
		setStyle("-fx-bar-color:"+UI.web(color)+";");
	}
	
	@Override
	public void layoutChartChildren() {
		Data<Scrip,Bar> data = getData();
		Bar bar = (Bar)data.getExtraValue();
		
		double high = getDisplayPosition(bar.getHigh());
		double low = getDisplayPosition(bar.getLow());
		Rectangle rectangle = getRectangle();
		rectangle.setY(high);
		rectangle.setHeight(low - high);
		double x = rectangle.getX();
		double width = rectangle.getWidth();
		
		box.resizeRelocate(x, high, width, rectangle.getHeight());
	}

}
